import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../model/cartInfo.dart';
import '../provider/cart.dart';
import './cart_page/cart_item.dart';
import './cart_page/cart_bottom.dart';

class CartPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('购物车'),
      ),
      body: FutureBuilder(
        future: _getCartInfo(context),
        builder: (context, snapshot) {
          if(snapshot.hasData) {
            return Consumer<CartProvider>(
              builder: (context, CartProvider cartProvider, child) {
                List<CartInfoMode> cartList = cartProvider.cartList;
                return Stack(
                  children: <Widget>[
                    ListView.builder(
                      itemCount: cartList.length,
                      itemBuilder: (context, index) {
                        CartInfoMode item = cartList[index];
                        return CartItem(item: item);
                      }
                    ),
                    Positioned(
                      left: 0,
                      bottom: 0,
                      child: CartBottom(cartProvider: cartProvider),
                    ),
                  ],
                );
              },
            );
          } else {
            return Text('正在加载...');
          }
        },
      ),
    );
  }

  // 获取购物车数据
  Future _getCartInfo(BuildContext context) async {
    await Provider.of<CartProvider>(context, listen: false).getCartInfo();
    return 'success';
  }
}